<?php
	//TBC: ham tao combo - ten bang, ten truong la gia tri, ten truong hien thi
	//dieu kien, ten cua combo trong form, index la gia tri ban dau duoc hien thi tren combo
function createCombo($table, $valueField, $textField, $where="", $name, $index){

	global $conn;

	$ret = array();

	//$hide = "status=0";

	$where = $where!="" ? $where : "1=1";

	$result = @mysql_query("select $valueField,$textField from $table where $where",$conn);
	
	echo '<select size="1" name="'.$name.'">';
	
	while($row=@mysql_fetch_assoc($result)){

		//$ret[] = array($row[$valueField],$row[$textField]);
		
		$selected = $row[$valueField] == $index ? 'selected' : '';
		
		echo "<option value = '".$row[$valueField]."' ".$selected.">".$row[$textField]."</option>";
	}

	echo '</select>';
 }
?>
<script language="javascript">
function btnSave_onclick(){
	
	return true;
}
</script>

<?php
	$errMsg ='';
	$file_path = "../upload/files/";
	//$arr = createCombo("tbl_theloai", "id", "tentheloai", "");
	//var_dump($arr);	
	//exit;
?>

<?php

if (isset($_POST['btnSave'])){
	$name          = isset($_POST['txtName']) ? trim($_POST['txtName']) : '';
	$theloaiid          = isset($_POST['cbTheloai']) ? trim($_POST['cbTheloai']) : '';
	$content          = isset($_POST['txtContent']) ? trim($_POST['txtContent']) : '';
	$file          = isset($_FILES["fFile"]["name"]) ? trim($_FILES["fFile"]["name"]) : '';
	$nxb = isset($_POST['txtNXB']) ? trim($_POST['txtNXB']) : '';
	
	if ($name=="") $errMsg .= "Hãy nhập tên danh mục !<br>";
	$errMsg .= checkUpload($_FILES['fFile'],".doc;.pdf;.xls;.docx",0, 0);
	if ($errMsg==''){
		if (!empty($_POST['id'])){
			$oldid = $_POST['id'];
			$sql = "update tbl_document set name='".$name."',theloai_id='".$theloaiid."', content='".$content."', nhaxuatban='".$nxb."' where id=".$oldid;
		}else{
			$sql = "insert into tbl_document (name, theloai_id, content, file, nhaxuatban, date_added, last_modified)";
			$sql .="	values ('".$name."', '".$theloaiid."', '".$content."', '".$file."', '".$nxb."', now(), now())";
		}
		echo $sql;
		
		if (mysql_query($sql,$conn)){
				//upload file 
				@chmod($file_path, 0777);
				move_uploaded_file($_FILES["fFile"]["tmp_name"],  $file_path. $_FILES["fFile"]["name"]);
				echo '<script>window.location="./?act=doc&cat='.$_REQUEST['cat'].'&page='.$_REQUEST['page'].'&code=1"</script>';
			}else{
				$errMsg = "Không thể cập nhật !";
		}
	}

	
		
}else{
	if (isset($_GET['id'])){
		$oldid=$_GET['id'];
		$page = $_GET['page'];
		$sql = "select * from tbl_document where id=".$oldid;
		if ($result = mysql_query($sql,$conn)) {
			$row=mysql_fetch_array($result);
			$name          = $row['name'];			
			$theloaiid = $row['theloai_id'];
			$file =$row['file'];
			$nxb = $row['nhaxuatban'];
			
		}
	}
}

?>

<?php
	//$arraySourceCombo    = getArrayCombo('tbl_theloai','id','tentheloai','');

?>
<form method="post" name="frmForm" enctype="multipart/form-data" action="./">

<input type="hidden" name="act" value="doc_m">
<input type="hidden" name="id" value="<?=$_REQUEST['id']?>">
<input type="hidden" name="page" value="<?=$_REQUEST['page']?>">
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#0069A8" width="100%">
	<tr>
    	<td width="45%">
    		<table border="0" cellpadding="2" bordercolor="#111111" width="100%" cellspacing="0">
				<tr><td height="10"></td></tr>
				<!--<tr>
					<td colspan="3" align="center">
						<table width="100%">
							<? if($image!='' || $image_large!=''){?>
							<tr>
								<td width="15%"></td>
								<td width="40%" align="center" class="smallfont">
<? if ($image!=''){ echo '<img border="0" src="../'.$image.'"><br><br>Hình (kích thước nhỏ)';}?>
								</td>
								
								<td width="40%" align="center" class="smallfont">
<? if ($image_large!=''){ echo '<img border="0" src="../'.$image_large.'"><br><br>Hình (kích thước lớn)';}?>
								</td>
								<td width="15%"></td>
							</tr>
							<? }else{echo '<tr><td colspan="3" class="smallfont" align="center">Chưa có hình ảnh !</td></tr>';}?>
							<tr><td colspan="4" height="10"></td></tr>
							<tr><td colspan="4" height="1" bgcolor="#999999"></td></tr>
							<tr><td colspan="4" height="10"></td></tr>
						</table>
					</td>
				</tr>-->
        										
				<tr>
					<td width="15%" class="smallfont" align="right">Tiêu đề</td>
					<td width="1%" class="smallfont" align="center"><font color="#FF0000">*</font></td>
					<td width="83%" class="smallfont">
						<input value="<?=$name?>" type="text" name="txtName" class="textbox" size="34">
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Tên danh mục</td>
					<td width="1%" class="smallfont" align="center"><font color="#FF0000"></font></td>
					<td width="83%" class="smallfont">
						<?createCombo('tbl_theloai', 'id', 'tentheloai', '', 'cbTheloai', $theloaiid)?>
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Nội dung</td>
					<td width="1%" class="smallfont" align="center"><font color="#FF0000"></font></td>
					<td width="83%" class="smallfont">
						<textarea value="<?=$content?>" name="txtContent" class="textbox" size="34"></textarea>
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">File</td>
					<td width="1%" class="smallfont" align="center"><font color="#FF0000">*</font></td>
					<td width="83%" class="smallfont">
						<input type="file" name="fFile" class="textbox" />
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Nhà xuất bản</td>
					<td width="1%" class="smallfont" align="center"><font color="#FF0000"></font></td>
					<td width="83%" class="smallfont">
						<input value="<?=$nxb?>" type="text" name="txtNXB" class="textbox" size="34">
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont"></td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<input type="submit" name="btnSave" VALUE="Cập nhật" class=button onclick="return btnSave_onclick()">
						<input type="reset" class=button value="Nhập lại">
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</form>
<? if($errMsg!=''){echo '<p align=center class="err">'.$errMsg.'<br></p>';}?>